package com.project.server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.common.api.Result;
import com.project.server.entity.Article;
import com.project.server.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/list")
    public Result<IPage<Article>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Integer status) {
        return Result.success(articleService.getArticlePage(current, size, title, categoryId, status));
    }

    @GetMapping("/{id}")
    public Result<Article> getById(@PathVariable Long id) {
        return Result.success(articleService.getById(id));
    }

    @PostMapping
    public Result<Boolean> add(@RequestBody Article article) {
        return Result.success(articleService.save(article));
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody Article article) {
        return Result.success(articleService.updateById(article));
    }

    @PostMapping("/updateById/{id}")
    public Result<Boolean> updateById(@PathVariable Long id) {
        Article article = articleService.getById(id);
        // 已发布
        article.setStatus(1);
        return Result.success(articleService.updateById(article));
    }
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(articleService.removeById(id));
    }


    @GetMapping("/recommend")
    public Result<List<Article>> getRecommend(
            @RequestParam(defaultValue = "5") Integer limit) {
        return Result.success(articleService.getRecommendArticles(limit));
    }

    @GetMapping("/stats")
    public Result<Map<String, Object>> getStats() {
        return Result.success(articleService.getArticleStats());
    }
}
